


#include <material_editor/materialview.h>
#include <material_editor/programeditor.h>
#include <genericdeletedialog.h>
#include <genericrenamedialog.h>
#include <genericsavedialog.h>
#include <session.h>



void MaterialView::ProgramEdit (Program *program)
{
  if (!OpenEditor(program))
    {
      InsertEditor(new ProgramEditor (program, this));
    }
}



void MaterialView::ProgramEditCurrent ()
{
  ProgramItem* item = get_current_item<ProgramItem>(_overview);
  if (!item)
    {
      return;
    }
  ProgramEdit (item->GetProgram());
}



void MaterialView::ProgramAdd()
{
  CategoryItem* item = get_current_item<CategoryItem>(_overview);
  if (!item)
    {
      return;
    }

  GenericRenameDialog dlg (Session::Get()->GetMainWindow());
  dlg.SetType("Create new program", "");
  dlg.SetName("New Program");
  dlg.SetCategory(item->GetCategory());
  if (dlg.exec() == QDialog::Rejected)
    {
      return;
    }


  _selectNewItems = true;
  Program* program = _manager->CreateProgram();
  program->SetName(dlg.GetName());
  program->SetCategory(dlg.GetCategory());

  if (_manager->AddProgram(program))
    {
      ProgramEdit(program);
    }
  _selectNewItems = false;
}

void MaterialView::ProgramAdded(Program *program)
{
  AddItem (new ProgramItem(this, program), program);
}

void MaterialView::EditProgram(Program *program)
{
  ProgramEdit(program);
}
